<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card" style="margin-top: 20px" shadow="never">
      <template #header>
        <el-row type="flex" justify="space-between">
          <el-col :span="4">共{{ total }}条记录</el-col>
          <el-col :span="2">
            <el-button
              type="primary"
              round
              size="small"
              class="el-icon-edit"
              @click="add"
              >添加面经</el-button
            >
          </el-col>
        </el-row>
      </template>
      <template>
        <el-table :data="list">
          <el-table-column
            label="标题"
            prop="stem"
            align="center"
          ></el-table-column>
          <el-table-column
            label="作者"
            prop="creator"
            align="center"
          ></el-table-column>
          <el-table-column
            label="点赞"
            prop="likeCount"
            width="100"
            align="center"
          ></el-table-column>
          <el-table-column
            label="浏览数"
            prop="views"
            width="100"
            align="center"
          ></el-table-column>
          <el-table-column
            label="更新时间"
            prop="createdAt"
            align="center"
          ></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <i class="el-icon-view" @click="preview(scope.row)"></i>
              <i class="el-icon-edit" @click="edit(scope.row)"></i>
              <i class="el-icon-delete" @click="del(scope.row.id)"></i>
            </template>
          </el-table-column>
        </el-table>
        <div style="text-align: center; margin-top: 20px">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :page-size="params.pageSize"
            @current-change="handleCurrentChange"
          >
          </el-pagination>
        </div>
      </template>
    </el-card>
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      direction="rtl"
      size="60%"
      @close="handleClose"
    >
      <div v-if="isPreview">
        <h3>{{ form.stem }}</h3>
        <hr />
        <div v-html="form.content"></div>
      </div>
      <el-form
        v-else
        label-width="80px"
        :model="form"
        :rules="rules"
        :ref="ref"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor
            v-model="form.content"
            @blur="$refs.formRef?.validateField('content')"
          ></quillEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

import { quillEditor } from "vue-quill-editor";

import {
  getInterviewList,
  addInterview,
  editInterview,
  deleteInterview,
} from "@/api/interview";

//import drawers from "../../components/drawers.vue";
export default {
  components: {
    quillEditor,
  },
  name: "ArticleIndex",
  data() {
    return {
      // 只有写在data中的数据才是响应式的，才能在模板中使用
      list: [],
      params: {
        current: 1, //当前默认显示第一页数据
        pageSize: 5, //每页条数是5条
      },
      total: 100,
      drawer: false,
      form: {
        stem: "",
        content: "",
      },
      title: "添加面经",
      isPreview: false,
      ref: "formRef",
      rules: {
        stem: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
    };
  },
  methods: {
    async getInterviewList() {
      const {
        data: { data },
      } = await getInterviewList(this.params);
      //console.log(data);
      this.list = data.rows;
      this.total = data.total;
      //console.log(this.list);
    },

    handleCurrentChange(current) {
      this.params.current = current;
      this.getInterviewList();
    },
    handleClose() {
      // console.log(drawer);
      this.isPreview = false;
      //可选链（高级玩法）
      this.$refs.formRef?.resetFields();
      //为了避免添加数据和编辑数据混了
      this.form = {
        stem: "",
        content: "",
      };
    },
    async save() {
      try {
        await this.$refs.formRef?.validate();
        if (this.title === "添加面经") {
          await addInterview(this.form);
        } else {
          // console.log('编辑保存数据');
          await editInterview({
            id: this.form.id,
            stem: this.form.stem,
            content: this.form.content,
          });
        }
        this.getInterviewList();
        this.drawer = false;
      } catch (err) {
        console.log(err);
      }
    },
    add() {
      this.title = "添加面经";
      this.drawer = true;
      // this.isPreview = true;
      //console.log(this.drawer);
    },
    edit(row) {
      this.title = "修改面经";
      this.drawer = true;
      // this.isPreview = true;
      //console.log(row);
      //this.form= row
      this.form = { ...row }; //保证页面表格数据和表单数据是互相独立的
    },
    async del(id) {
      //console.log(id);
      try {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
        await deleteInterview(id);
        this.getInterviewList();
        this.$message.success("删除成功");
      } catch (err) {
        console.log(err);
      }
    },
    preview(row) {
      this.title = "预览面经";
      this.isPreview = true;
      this.drawer = true;
      this.form = row;
    },
  },
  created() {
    this.getInterviewList();
  },
};
</script>

<style scoped>
.cell i {
  font-size: 20px;
  margin-right: 6px;
}
.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: pink;
  color: #fff;
}
.el-drawer__body > * {
  padding: 20px;
}
::v-deep .ql-editor {
  min-height: 250px;
}
</style>
